package mao.mashibing.juc.jucutil;

import java.util.concurrent.Exchanger;

/*
user：mao
data：2020/11/22
explain：
*/
public class TestExchanger {
    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(()->{
           String s = "T1";
           try {
               System.out.println(Thread.currentThread().getName()+":"+s);
               s = exchanger.exchange(s);
               System.out.println(Thread.currentThread().getName()+":"+s);
           }catch (InterruptedException e){
               e.printStackTrace();
           }
        },"T1").start();

        new Thread(()->{
            String s = "T2";
            try {
                System.out.println(Thread.currentThread().getName()+":"+s);
                s = exchanger.exchange(s);
                System.out.println(Thread.currentThread().getName()+":"+s);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        },"T2").start();
    }
}
